﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Ch13CardLib;
using Ch13CardLib.enums;
using KarliCardsGui.Converters;
using KarliCardsGui.ViewModels;

namespace KarliCardsGui
{
    /// <summary>
    /// Interaction logic for CardControl.xaml
    /// </summary>
    public partial class CardControl : UserControl
    {
        public event EventHandler<RoutedEventArgs> CardClicked;

        public CardControl()
        {
            InitializeComponent();
        }

        #region 依赖属性
        public static readonly DependencyProperty SuitProperty = 
            DependencyProperty.Register(
                nameof(Suit),
                typeof(Suit),
                typeof(CardControl),
                new PropertyMetadata(Suit.Club)
            );

        public static readonly DependencyProperty RankProperty = 
            DependencyProperty.Register(
                nameof(Rank),
                typeof(Rank),
                typeof(CardControl),
                new PropertyMetadata(Rank.Ace)
            );

        public static readonly DependencyProperty IsFaceUpProperty = 
            DependencyProperty.Register(
                nameof(IsFaceUp),
                typeof(bool),
                typeof(CardControl),
                new PropertyMetadata(true)
            );
        #endregion

        #region CLR 属性包装器
        public Suit Suit
        {
            get => (Suit)GetValue(SuitProperty);
            set => SetValue(SuitProperty, value);
        }

        public Rank Rank
        {
            get => (Rank)GetValue(RankProperty);
            set => SetValue(RankProperty, value);
        }

        public bool IsFaceUp
        {
            get => (bool)GetValue(IsFaceUpProperty);
            set => SetValue(IsFaceUpProperty, value);
        }
        #endregion

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var viewModel = DataContext as CardViewModel;
            if (viewModel != null && viewModel.IsClickable && !viewModel.IsFaceUp)
            {
                CardClicked?.Invoke(this, e);
            }
        }
    }
}
